org.springframework.data.document.mongodb.convert
Class SimpleMongoConverter

java.lang.Object
  extended by org.springframework.data.document.mongodb.convert.SimpleMongoConverter
All Implemented Interfaces:
MongoConverter, MongoReader<java.lang.Object>, MongoWriter<java.lang.Object>

public class SimpleMongoConverter
extends java.lang.Object
implements MongoConverter

Basic MongoConverter implementation to convert between domain classes and DBObjects.

Author:
Mark Pollack, Thomas Risberg, Oliver Gierke

Field Summary
private  GenericConversionService conversionService
           
private static Log LOG
           
private static java.util.List<java.lang.Class<?>> MONGO_TYPES
           
private static java.util.Set<java.lang.String> SIMPLE_TYPES
           
 
Constructor Summary
SimpleMongoConverter()
          Creates a SimpleMongoConverter.
 
Method Summary
 void afterPropertiesSet()
           
 ObjectId convertObjectId(java.lang.Object id)
          Returns the ObjectId instance for the given id.
<T> T
convertObjectId(ObjectId id, java.lang.Class<T> targetType)
          Converts the given ObjectId to the given target type.
protected  java.util.Map<java.lang.String,java.lang.Object> createMap()
          Create a Map instance.
protected  MongoBeanWrapper createWrapper(java.lang.Object target, boolean fieldAccess)
          Callback to allow customizing creation of a MongoBeanWrapper.
private  java.lang.Class<?> getCustomTargetType(java.lang.Object obj)
          Returns whether the ConversionService has a custom Converter registered that can convert the given object into one of the types supported by MongoDB.
 java.util.List<java.lang.Class<?>> getGenericParameters(java.lang.reflect.Type genericParameterType)
           
private  void initializeConverters()
          Initializes additional converters that handle ObjectId conversion.
protected static boolean isSimpleType(java.lang.Class<?> propertyType)
           
<S> S
read(java.lang.Class<S> clazz, DBObject source)
          Ready from the native MongoDB DBObject representation to an instance of the class T.
private  java.util.Collection<java.lang.Object> readCollection(MongoPropertyDescriptors.MongoPropertyDescriptor descriptor, java.util.Collection<?> values)
          Reads the given collection values (that are DBObjects potentially) into a Collection of domain objects.
private  java.lang.Object readCompoundValue(MongoPropertyDescriptors.MongoPropertyDescriptor pd, DBObject dbo)
          Reads a compound value from the given DBObject for the given property.
protected  java.util.Map<?,?> readMap(MongoPropertyDescriptors.MongoPropertyDescriptor pd, DBObject dbo, java.lang.Class<?> targetType)
          Reads every key/value pair from the DBObject into a Map instance.
 void setConverters(java.util.Set<?> converters)
          Add custom Converter or ConverterFactory instances to be used that will take presidence over using object traversal to convert and object to/from DBObject
 void write(java.lang.Object obj, DBObject dbo)
          Write the given object of type T to the native MongoDB object representation DBObject.
protected  void writeArray(DBObject dbo, java.lang.String keyToUse, java.lang.Object[] array)
          Writes the given array to the given DBObject.
private  void writeCompoundValue(DBObject dbo, java.lang.String keyToUse, java.lang.Object value)
          Writes the given CompoundComparator value to the given DBObject.
protected  void writeMap(DBObject dbo, java.lang.String mapKey, java.util.Map<java.lang.String,java.lang.Object> map)
          Writes the given Map to the given DBObject.
private  void writeValue(DBObject dbo, java.lang.String keyToUse, java.lang.Object value)
          Writes the given value to the given DBObject.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

private static final Log LOG

MONGO_TYPES

private static final java.util.List<java.lang.Class<?>> MONGO_TYPES

SIMPLE_TYPES

private static final java.util.Set<java.lang.String> SIMPLE_TYPES

conversionService

private final GenericConversionService conversionService
Constructor Detail

SimpleMongoConverter

public SimpleMongoConverter()
Creates a SimpleMongoConverter.

Method Detail

initializeConverters

private void initializeConverters()
Initializes additional converters that handle ObjectId conversion. Will register converters for supported id types if none are registered for those conversion already. GenericConversionService is configured.


setConverters

public void setConverters(java.util.Set<?> converters)
Add custom Converter or ConverterFactory instances to be used that will take presidence over using object traversal to convert and object to/from DBObject

Parameters:
converters -

write

public void write(java.lang.Object obj,
                  DBObject dbo)
Description copied from interface: MongoWriter
Write the given object of type T to the native MongoDB object representation DBObject.

Specified by:
write in interface MongoWriter<java.lang.Object>
Parameters:
obj - The object to convert to a DBObject
dbo - The DBObject to use for writing.

writeValue

private void writeValue(DBObject dbo,
                        java.lang.String keyToUse,
                        java.lang.Object value)
Writes the given value to the given DBObject. Will skip null values.

Parameters:
dbo -
keyToUse -
value -

writeCompoundValue

private void writeCompoundValue(DBObject dbo,
                                java.lang.String keyToUse,
                                java.lang.Object value)
Writes the given CompoundComparator value to the given DBObject.

Parameters:
dbo -
keyToUse -
value -

getCustomTargetType

private java.lang.Class<?> getCustomTargetType(java.lang.Object obj)
Returns whether the ConversionService has a custom Converter registered that can convert the given object into one of the types supported by MongoDB.

Parameters:
obj -
Returns:

writeMap

protected void writeMap(DBObject dbo,
                        java.lang.String mapKey,
                        java.util.Map<java.lang.String,java.lang.Object> map)
Writes the given Map to the given DBObject.

Parameters:
dbo -
mapKey -
map -

writeArray

protected void writeArray(DBObject dbo,
                          java.lang.String keyToUse,
                          java.lang.Object[] array)
Writes the given array to the given DBObject.

Parameters:
dbo -
keyToUse -
array -

read

public <S> S read(java.lang.Class<S> clazz,
                  DBObject source)
Description copied from interface: MongoReader
Ready from the native MongoDB DBObject representation to an instance of the class T. The given type has to be the starting point for marshalling the DBObject into it. So in case there's no real valid data inside DBObject for the given type, just return an empty instance of the given type.

Specified by:
read in interface MongoReader<java.lang.Object>
Parameters:
clazz - the type of the return value
source - theDBObject
Returns:
the converted object

readCollection

private java.util.Collection<java.lang.Object> readCollection(MongoPropertyDescriptors.MongoPropertyDescriptor descriptor,
                                                              java.util.Collection<?> values)
Reads the given collection values (that are DBObjects potentially) into a Collection of domain objects.

Parameters:
descriptor -
values -
Returns:

readCompoundValue

private java.lang.Object readCompoundValue(MongoPropertyDescriptors.MongoPropertyDescriptor pd,
                                           DBObject dbo)
Reads a compound value from the given DBObject for the given property.

Parameters:
pd -
dbo -
Returns:

createMap

protected java.util.Map<java.lang.String,java.lang.Object> createMap()
Create a Map instance. Will return a HashMap by default. Subclasses might want to override this method to use a custom Map implementation.

Returns:

readMap

protected java.util.Map<?,?> readMap(MongoPropertyDescriptors.MongoPropertyDescriptor pd,
                                     DBObject dbo,
                                     java.lang.Class<?> targetType)
Reads every key/value pair from the DBObject into a Map instance.

Parameters:
pd -
dbo -
targetType -
Returns:

isSimpleType

protected static boolean isSimpleType(java.lang.Class<?> propertyType)

createWrapper

protected MongoBeanWrapper createWrapper(java.lang.Object target,
                                         boolean fieldAccess)
Callback to allow customizing creation of a MongoBeanWrapper.

Parameters:
target - the target object to wrap
fieldAccess - whether to use field access or property access
Returns:

getGenericParameters

public java.util.List<java.lang.Class<?>> getGenericParameters(java.lang.reflect.Type genericParameterType)

convertObjectId

public <T> T convertObjectId(ObjectId id,
                             java.lang.Class<T> targetType)
Description copied from interface: MongoConverter
Converts the given ObjectId to the given target type.

Specified by:
convertObjectId in interface MongoConverter
Type Parameters:
T - the actual type to create
Parameters:
id - the source ObjectId
targetType - the target type to convert the ObjectId to
Returns:

convertObjectId

public ObjectId convertObjectId(java.lang.Object id)
Description copied from interface: MongoConverter
Returns the ObjectId instance for the given id.

Specified by:
convertObjectId in interface MongoConverter
Returns:

afterPropertiesSet

public void afterPropertiesSet()